' COI Disclosure Statement - Requirement Script - VRG95
Option Strict Off
imports System
imports Humans=PHSRAG.Insight21.Humans
imports COI = PHSRAG.Insight21.COI
imports PHSRAG.Insight21.BusinessObjects
imports PHSRAG.Insight21.FormsManagement
imports System.Collections
imports Utility=PHSRAG.Utility
imports PHSRAG.Insight21.Policy

Module Script
    Sub Main()
							
		Dim answers As System.Collections.ArrayList
		Dim answer As Answer
		
		Dim fiduciaryAnswer as boolean = false
		Dim compensation5KAnswer as boolean = false
		Dim questionnaireAnswer as QuestionnaireAnswer
		
		Dim _cachePolicy as ICachePolicy
		
		currentCOI.LoadQuestionnaireAnswers()

		'answers = questAnswer.GetAnswers("Fiduciary")
		'If ( answers.Count > 0 ) Then
			'answer = answers(0)
			'If(answer.Value = "Y") Then
				'fiduciaryAnswer = true
			'End If
		'End If
		
		'answers = questAnswer.GetAnswers("Compensation5K")
		'If ( answers.Count > 0 ) Then
			'answer = answers(0)
			'If(answer.Value = "Y") Then
				'compensation5KAnswer = true
			'End If							
		'End If
		
		'If (fiduciaryAnswer OR compensation5KAnswer) Then
			'currentCOI.HasOutsideActivities = "Y"
			''Get the active questionnaireversion no.
			'dim outsideActivitiesQV as QuestionnaireVersion = _
					'Questionnaire.GetActiveVersion(cachePolicy, "Your Outside Activities", _
							'PHSRAG.Insight21.FormsManagement.Constants.QuestionnaireType.COI)

		answers = questAnswer.GetAnswers("OutsideActivity")
		If ( answers.Count > 0 ) Then
			answer = answers(0)
			If(answer.Value = "Y") Then
				currentCOI.HasOutsideActivities = "Y"
				'Get the active questionnaireversion no.
				dim outsideActivitiesQV as QuestionnaireVersion = _
						Questionnaire.GetActiveVersion(cachePolicy, "Your Outside Activities", _
							PHSRAG.Insight21.FormsManagement.Constants.QuestionnaireType.COI)
	
			answers = questAnswer.GetAnswers("ListOutsideActivities")

			'Delete the OA which are no longer applicable
			For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
				If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_OA" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Outside Activities") Then
					dim found as boolean = false
					For Each answer in answers
						dim oa as string() = answer.Value.Split("|")
						if (questionnaireAnswer.Name = oa(1)) then
							found = true
							exit for
						end if
					Next
					if not found then
						currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
					end if
				End If
			Next	

			For Each answer in answers
				dim oa as string() = answer.Value.Split("|")
				'add OA questionnaire answer if it is not added yet
				dim found as boolean = false
				For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
					If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_OA" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Outside Activities") Then
						if (questionnaireAnswer.Name = oa(1)) then
							found = true
							exit for
						end if
					End If
				Next
				
				if not found then
					dim qa as QuestionnaireAnswer = new QuestionnaireAnswer(cachePolicy)
					qa.QuestionnaireVersionId = outsideActivitiesQV.Id
					qa.Name = oa(1)
					qa.ExternalId = oa(0)
					
					dim outsideEntityAnswer as new Answer(cachePolicy)
					outsideEntityAnswer.QuestionnaireAnswerId = qa.Id

					outsideEntityAnswer.Name = "OutsideEntityName"
					outsideEntityAnswer.Value = oa(1)
					
					qa.Answers.Add(outsideEntityAnswer)
					currentCOI.QuestionnaireAnswers.Add(qa)
					currentCOI.Save(connectionString) 'takes care of the mapping record
				end if								
			Next
		Else
			currentCOI.HasOutsideActivities = "N"
			For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
				If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_OA" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Outside Activities") Then
					currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
				End If
			Next	
		End If
		end if
		
		'Verifying if equity interests have to be disclosed needs to be filled out
		answers = questAnswer.GetAnswers("YourEquityInterests")
		If ( answers.Count > 0 ) Then
			answer = answers(0)
			If(answer.Value = "Y") Then
				currentCOI.HasEquityInterests = "Y"
				'Get the active questionnaireversion no.
				dim equityInterestQV as QuestionnaireVersion = _
						Questionnaire.GetActiveVersion(cachePolicy, "Your Equity Interests", _
							PHSRAG.Insight21.FormsManagement.Constants.QuestionnaireType.COI)
				
				answers = questAnswer.GetAnswers("ListEquityInterests")

				'Delete the EI which are no longer applicable
				For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
					If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_EI" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Equity Interests") Then
						dim found as boolean = false
						For Each answer in answers
							dim ei as string() = answer.Value.Split("|")
							if (questionnaireAnswer.Name = ei(1)) then
								found = true
								exit for
							end if
						Next
						if not found then
							currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
						end if
					End If
				Next	

				For Each answer in answers
					dim ei as string() = answer.Value.Split("|")
					'add OA questionnaire answer if it is not added yet
					dim found as boolean = false
					For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
						If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_EI" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Equity Interests") Then
							if (questionnaireAnswer.Name = ei(1)) then
								found = true
								exit for
							end if
						End If
					Next
					
					if not found then
						dim qa as QuestionnaireAnswer = new QuestionnaireAnswer(cachePolicy)
						qa.QuestionnaireVersionId = equityInterestQV.Id
						qa.Name = ei(1)
						qa.ExternalId = ei(0)
						
						dim outsideEntityAnswer as new Answer(cachePolicy)
						outsideEntityAnswer.QuestionnaireAnswerId = qa.Id

						outsideEntityAnswer.Name = "OutsideEntityName"
						outsideEntityAnswer.Value = ei(1)
						
						qa.Answers.Add(outsideEntityAnswer)
						currentCOI.QuestionnaireAnswers.Add(qa)
						currentCOI.Save(connectionString) 'takes care of the mapping record
					end if								
				Next
			Else
				currentCOI.HasEquityInterests = "N"
				For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
				If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_EI" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Equity Interests") Then
					currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
				End If
			Next
			End If							
		End If
		
		'Verifying if family associations have to be disclosed needs to be filled out
		Dim answersFamilyEquity As System.Collections.ArrayList
		Dim answerFamilyEquity As Answer
		
		answers = questAnswer.GetAnswers("YourFamily")
		answersFamilyEquity = questAnswer.GetAnswers("YourFamilyEquity")
		'YourFamilyEquity
		'ListFamilyEquity
		If ( answers.Count > 0 OR answersFamilyEquity.Count > 0) Then
			
			'answer = answers(0)
			
			IF (answers.count > 0) THEN
			    answer = answers(0)
			ELSE
			    answer = new Answer(_cachePolicy)
			END IF
            
            IF (answersFamilyEquity.Count > 0) THEN
                answerFamilyEquity = answersFamilyEquity(0)
            ELSE
                answerFamilyEquity = new Answer(_cachePolicy)
            END IF
            
            
            
			If(answer.Value = "Y" OR answerFamilyEquity.Value = "Y") Then
				currentCOI.HasFamilyAssociations = "Y"
				'Get the active questionnaireversion no.
				dim familyAssociationQV as QuestionnaireVersion = _
						Questionnaire.GetActiveVersion(cachePolicy, "Your Family's Associations and Equity Interests", _
							PHSRAG.Insight21.FormsManagement.Constants.QuestionnaireType.COI)
				
				answers = questAnswer.GetAnswers("ListFamilyAssociations")
				answersFamilyEquity = questAnswer.GetAnswers("ListFamilyEquity")
				
				'Club the two array lists
				Dim answer_yu as Answer
                For Each answer_yu IN answersFamilyEquity
                    Dim found as boolean = false
                    Dim answer1 as Answer
                    For Each answer1 IN answers
                        IF answer1.Value = answer_yu.Value Then
                            found = true
                            exit for
                        End If 
                    Next
                    If NOT found Then
                        answers.Add(answer_yu)
                    End If
                Next

				'Delete the OA which are no longer applicable
				For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
					If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_FA" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Family's Associations and Equity Interests") Then
						dim found as boolean = false
						For Each answer in answers
							dim fa as string() = answer.Value.Split("|")
							if (questionnaireAnswer.Name = fa(1)) then
								found = true
								exit for
							end if
						Next
						if not found then
							currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
						end if
					End If
				Next	

				For Each answer in answers
					dim fa as string() = answer.Value.Split("|")
					'add OA questionnaire answer if it is not added yet
					dim found as boolean = false
					For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
						If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_FA" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Family's Associations and Equity Interests") Then
							if (questionnaireAnswer.Name = fa(1)) then
								found = true
								exit for
							end if
						End If
					Next
					
					if not found then
						dim qa as QuestionnaireAnswer = new QuestionnaireAnswer(cachePolicy)
						qa.QuestionnaireVersionId = familyAssociationQV.Id
						qa.Name = fa(1)
						qa.ExternalId = fa(0)
						
						dim outsideEntityAnswer as new Answer(cachePolicy)
						outsideEntityAnswer.QuestionnaireAnswerId = qa.Id

						outsideEntityAnswer.Name = "OutsideEntityName"
						outsideEntityAnswer.Value = fa(1)
						
						
						qa.Answers.Add(outsideEntityAnswer)
						currentCOI.QuestionnaireAnswers.Add(qa)
						currentCOI.Save(connectionString) 'takes care of the mapping record
					end if								
				Next
			Else
				currentCOI.HasFamilyAssociations = "N"
				For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
				If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_FA" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Family's Associations and Equity Interests") Then
					currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
				End If
			Next
			End If							
		End If
		
		
		'Verifying if Royalties have to be disclosed needs to be filled out
		answers = questAnswer.GetAnswers("YourRoyalty")
		If ( answers.Count > 0 ) Then
			answer = answers(0)
			If(answer.Value = "Y") Then
				currentCOI.HasRoyalties = "Y"
				'Get the active questionnaireversion no.
				dim royaltyQV as QuestionnaireVersion = _
						Questionnaire.GetActiveVersion(cachePolicy, "Your Intellectual Property", _
							PHSRAG.Insight21.FormsManagement.Constants.QuestionnaireType.COI)
				
				answers = questAnswer.GetAnswers("ListRoyalties")

				'Delete the Royalties which are no longer applicable
				For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
					If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_R" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Intellectual Property") Then
						dim found as boolean = false
						For Each answer in answers
							dim royalties as string() = answer.Value.Split("|")
							if (questionnaireAnswer.Name = royalties(1)) then
								found = true
								exit for
							end if
						Next
						if not found then
							currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
						end if
					End If
				Next	

				For Each answer in answers
					dim royalties as string() = answer.Value.Split("|")
					'add Royalties questionnaire answer if it is not added yet
					dim found as boolean = false
					For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
						If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_R" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Intellectual Property") Then
							if (questionnaireAnswer.Name = royalties(1)) then
								found = true
								exit for
							end if
						End If
					Next
					
					if not found then
						dim qa as QuestionnaireAnswer = new QuestionnaireAnswer(cachePolicy)
						qa.QuestionnaireVersionId = royaltyQV.Id
						qa.Name = royalties(1)
						qa.ExternalId = royalties(0)
						
						dim outsideEntityAnswer as new Answer(cachePolicy)
						outsideEntityAnswer.QuestionnaireAnswerId = qa.Id

						outsideEntityAnswer.Name = "OutsideEntityName"
						outsideEntityAnswer.Value = royalties(1)
						
						
						qa.Answers.Add(outsideEntityAnswer)
						currentCOI.QuestionnaireAnswers.Add(qa)
						currentCOI.Save(connectionString) 'takes care of the mapping record
					end if								
				Next
			Else
				currentCOI.HasRoyalties = "N"
				For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
				If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_R" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Your Intellectual Property") Then
					currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
				End If
			Next
			End If							
		End If
		
		currentCOI.Save(connectionString, PHSRAG.Insight21.COI.Constants.COIDateType.None)
    End Sub
			
End Module